package com.company;



import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class EchoClient {
  public static final String HOST="localhost";       //连接主机
  public static final int PORT=9999;                 //绑定端口

  public static void main(String[] args) throws IOException {
    SocketChannel clientChannel = SocketChannel.open();
    clientChannel.connect(new InetSocketAddress(HOST,PORT));       //连接服务器端
    ByteBuffer buffer = ByteBuffer.allocate(50);                   //开辟缓存
    boolean flag=true;
    while (flag){
      buffer.clear();
      String msg = InputUtil.getString("请输入要发送的信息：");
      buffer.put(msg.getBytes());
      buffer.flip();
      clientChannel.write(buffer);
      buffer.clear();
      int readCount = clientChannel.read(buffer);                //读取服务器端响应
      buffer.flip();
      System.err.println(new String(buffer.array(),0,readCount));
      if ("exit".equals(msg)){                                   //结束指令
        flag=false;                                              //结束循环
      }
    }
   clientChannel.close();
  }
}
